package viper.diversity;


public class Dediversity {

	private static byte [] A;
	private static byte [] B;
	private static byte [] C;
	private static byte [] A_Xor_B;
	private static byte [] A_Xor_C;
	private static byte [] B_Xor_C;
	private static byte [][] Two_Three_x;
	private static byte [][] Four_Six_x;

	public static byte[][] undiversify(byte [] one, byte [] two,byte [] three){
		Two_Three_x= new byte [2][];
		if(one != null && two != null && three!= null){
			System.out.println("HERERE");
			Two_Three_x[0]=one;
			Two_Three_x[1]=two;
		}
		else if(one != null && two != null && three== null){
			Two_Three_x[0]=one;
			Two_Three_x[1]=two;
		}
		else if (one !=null && two == null && three !=null){
				Two_Three_x[0]=one;
				A_Xor_B= new byte[one.length];
				System.arraycopy(three,0,A_Xor_B,0,three.length);
				B = new byte [one.length];			
				for (int i =0;i<B.length;i++)
				{
					B[i]=(byte)(one[i]^A_Xor_B[i]);
				}
				Two_Three_x[1]=B;
			}
			else if (one!=null && two == null && three ==null){
				Two_Three_x[0]=one;
				Two_Three_x[1]=null;
			}
			else if(one==null&&two!=null && three != null){
				Two_Three_x[1]=two;
				A_Xor_B= new byte[two.length];
				System.arraycopy(three,0,A_Xor_B,0,three.length);
				A = new byte [two.length];			
				for (int i =0;i<A.length;i++)
				{
					A[i]=(byte)(two[i]^A_Xor_B[i]);
				}
				Two_Three_x[0]=A;
				
			}
			else if(one==null && two != null &&  three==null){
				Two_Three_x[0]=null;
				Two_Three_x[1]=two;
			}
			else if(one == null && two==null && three !=null){
				Two_Three_x[0]=null;
				Two_Three_x[1]=null;
			}
			else{
				Two_Three_x[0]=null;
				Two_Three_x[1]=null;
			}
			
			return Two_Three_x;
		}
		
	public static byte [][] undiversify(byte [] one, byte [] two, byte [] three, byte [] four, byte [] five, byte [] six){
		Four_Six_x= new byte [3][];
		if(one!=null && two!=null && three!=null){
			Four_Six_x[0]=one;
			Four_Six_x[1]=two;
			Four_Six_x[2]=three;
			return Four_Six_x;
		}
		else if (one!= null && two != null && five != null){
			Four_Six_x[0]=one;
			Four_Six_x[1]=two;
			A_Xor_C= new byte[one.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			C = new byte [two.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(one[i]^A_Xor_C[i]);
			}
			Four_Six_x[2]= C;
			return Four_Six_x;
		}
		else if (one!= null && two != null && six != null){
			Four_Six_x[0]=one;
			Four_Six_x[1]=two;
			B_Xor_C= new byte[one.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			C = new byte [two.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(two[i]^B_Xor_C[i]);
			}
			Four_Six_x[2]= C;
			return Four_Six_x;
		}
		else if (two!= null && three != null && four != null){
			Four_Six_x[1]=two;
			Four_Six_x[2]=three;
			A_Xor_B= new byte[two.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			A = new byte [two.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(two[i]^A_Xor_B[i]);
			}
			Four_Six_x[0]= A;
			return Four_Six_x;
		}
		else if (two!= null && three != null && five != null){
			Four_Six_x[1]=two;
			Four_Six_x[2]=three;
			A_Xor_C= new byte[two.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			A = new byte [two.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(three[i]^A_Xor_C[i]);
			}
			Four_Six_x[0]= A;
			return Four_Six_x;
		}
		else if (one!= null && three != null && four != null){
			Four_Six_x[0]=one;
			Four_Six_x[2]=three;
			A_Xor_B= new byte[one.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			B = new byte [one.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(one[i]^A_Xor_B[i]);
			}
			
			Four_Six_x[1]= B;
			return Four_Six_x;
		}
		else if (one!= null && three != null && six != null){
			Four_Six_x[0]=one;
			Four_Six_x[2]=three;
			B_Xor_C= new byte[one.length];
			System.arraycopy(six,0,B_Xor_C, 0,six.length);
			B = new byte [one.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(three[i]^B_Xor_C[i]);
			}
			
			Four_Six_x[1]= B;
			return Four_Six_x;
		}
		else if (one!= null && four != null && five != null){
			Four_Six_x[0]=one;
			A_Xor_B= new byte[one.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			B = new byte [one.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(one[i]^A_Xor_B[i]);
			}
			A_Xor_C= new byte[one.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			C = new byte [one.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(one[i]^A_Xor_C[i]);
			}
			
			Four_Six_x[1]= B;
			Four_Six_x[2]= C;
			return Four_Six_x;
		}
		else if (one!= null && four != null && six != null){
			Four_Six_x[0]=one;
			A_Xor_B= new byte[one.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			B = new byte [one.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(one[i]^A_Xor_B[i]);
			}
			B_Xor_C= new byte[one.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			C = new byte [one.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(B[i]^B_Xor_C[i]);
			}
			
			Four_Six_x[1]= B;
			Four_Six_x[2]= C;
			return Four_Six_x;
		}
		else if (one!= null && five != null && six != null){
			Four_Six_x[0]=one;
			A_Xor_C= new byte[one.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			C = new byte [one.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(one[i]^A_Xor_C[i]);
			}
			B_Xor_C= new byte[one.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			B = new byte [one.length];			
			for (int i =0;i<C.length;i++)
			{
				B[i]=(byte)(C[i]^B_Xor_C[i]);
			}
			
			Four_Six_x[1]= B;
			Four_Six_x[2]= C;
			return Four_Six_x;
		}
		else if (two!= null && four != null && five != null){
			Four_Six_x[1]=two;
			A_Xor_B= new byte[two.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			A = new byte [two.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(two[i]^A_Xor_B[i]);
			}
			A_Xor_C= new byte[two.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			C = new byte [two.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(A[i]^A_Xor_C[i]);
			}
			
			Four_Six_x[0]= A;
			Four_Six_x[2]= C;
			return Four_Six_x;
		}
		else if (two!= null && four != null && six != null){
			Four_Six_x[1]=two;
			A_Xor_B= new byte[two.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			A = new byte [two.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(two[i]^A_Xor_B[i]);
			}
			B_Xor_C= new byte[two.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			C = new byte [two.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(two[i]^B_Xor_C[i]);
			}
			
			Four_Six_x[0]= A;
			Four_Six_x[2]= C;
			return Four_Six_x;
		}
		else if (two!= null && five != null && six != null){
			Four_Six_x[1]=two;
			B_Xor_C= new byte[two.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			C = new byte [two.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(two[i]^B_Xor_C[i]);
			}
			A_Xor_C= new byte[two.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			A = new byte [two.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(C[i]^A_Xor_C[i]);
			}
			
			
			Four_Six_x[0]= A;
			Four_Six_x[2]= C;
			return Four_Six_x;
		}
		else if (three!= null && four != null && five != null){
			Four_Six_x[2]=three;
			A_Xor_C= new byte[three.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			A = new byte [three.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(three[i]^A_Xor_C[i]);
			}
			A_Xor_B= new byte[three.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			B = new byte [three.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(A[i]^A_Xor_B[i]);
			}
			
			
			Four_Six_x[0]= A;
			Four_Six_x[1]= B;
			return Four_Six_x;
		}
		else if (three!= null && five != null && six != null){
			Four_Six_x[2]=three;
			A_Xor_C= new byte[three.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			A = new byte [three.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(three[i]^A_Xor_C[i]);
			}
			B_Xor_C= new byte[three.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			B = new byte [three.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(three[i]^B_Xor_C[i]);
			}
			
			
			Four_Six_x[0]= A;
			Four_Six_x[1]= B;
			return Four_Six_x;
		}
		else if (three!= null && four != null && six != null){
			Four_Six_x[2]=three;
			B_Xor_C= new byte[three.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			B = new byte [three.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(three[i]^B_Xor_C[i]);
			}
			A_Xor_B= new byte[three.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			A = new byte [three.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(B[i]^A_Xor_B[i]);
			}		
			
			Four_Six_x[0]= A;
			Four_Six_x[1]= B;
			return Four_Six_x;
		}
		else if (two != null && three != null){
			Four_Six_x[2] = three;
			Four_Six_x[0]= null;
			Four_Six_x[1]= two;
			return Four_Six_x;
		}
		else if (one != null && three != null){
			Four_Six_x[2] = three;
			Four_Six_x[0]= one;
			Four_Six_x[1]= null;
			return Four_Six_x;
		}
		else if (one != null && two != null){
			Four_Six_x[2] = null;
			Four_Six_x[0]= one;
			Four_Six_x[1]= two;
			return Four_Six_x;
		}
		
		else if (one!= null && four != null){
			Four_Six_x[0]=one;
			A_Xor_B= new byte[one.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			B = new byte [one.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(one[i]^A_Xor_B[i]);
			}		
			Four_Six_x[1]= B;
			Four_Six_x[2]=null;
			return Four_Six_x;
		}
		else if (one!= null && five != null){
			Four_Six_x[0]=one;
			A_Xor_C= new byte[one.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			C = new byte [one.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(one[i]^A_Xor_C[i]);
			}		
			Four_Six_x[1]= null;
			Four_Six_x[2]=C;
			return Four_Six_x;
		}
		else if (two!= null && six != null){
			Four_Six_x[1]=two;
			B_Xor_C= new byte[two.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			C = new byte [two.length];			
			for (int i =0;i<C.length;i++)
			{
				C[i]=(byte)(two[i]^B_Xor_C[i]);
			}		
			Four_Six_x[0]= null;
			Four_Six_x[2]=C;
			return Four_Six_x;
		}
		else if (two!= null && four != null){
			Four_Six_x[1]=two;
			A_Xor_B= new byte[two.length];
			System.arraycopy(four,0,A_Xor_B,0,four.length);
			A = new byte [two.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(two[i]^A_Xor_B[i]);
			}		
			Four_Six_x[2]= null;
			Four_Six_x[0]=A;
			return Four_Six_x;
		}
		else if (three!= null && five != null){
			Four_Six_x[2]=three;
			A_Xor_C= new byte[three.length];
			System.arraycopy(five,0,A_Xor_C,0,five.length);
			A = new byte [three.length];			
			for (int i =0;i<A.length;i++)
			{
				A[i]=(byte)(three[i]^A_Xor_C[i]);
			}		
			Four_Six_x[1]= null;
			Four_Six_x[0]=A;
			return Four_Six_x;
		}
		else if (three!= null && six != null){
			Four_Six_x[2]=three;
			B_Xor_C= new byte[three.length];
			System.arraycopy(six,0,B_Xor_C,0,six.length);
			B = new byte [three.length];			
			for (int i =0;i<B.length;i++)
			{
				B[i]=(byte)(three[i]^B_Xor_C[i]);
			}		
			Four_Six_x[0]= null;
			Four_Six_x[2]=B;
			return Four_Six_x;
		}
		else if (one!= null){
			Four_Six_x[0]=one;	
			Four_Six_x[1]= null;
			Four_Six_x[2]=null;
			return Four_Six_x;
		}
		else if (two!= null){
			Four_Six_x[0]=null;	
			Four_Six_x[1]= two;
			Four_Six_x[2]=null;
			return Four_Six_x;
		}
		else if (three!= null){
			Four_Six_x[0]=null;	
			Four_Six_x[1]= null;
			Four_Six_x[2]=three;
			return Four_Six_x;
		}
		
		else if (one == null && two == null && three == null){
			Four_Six_x[2] = null;
			Four_Six_x[0]= null;
			Four_Six_x[1]= null;
			return Four_Six_x;
		}
		else{}
		return null;
		
	}
	}	
			